<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
	  <el-form-item label="楼宇" prop="floorId">
	    <el-select v-model="queryParams.floorId"
	     class="width100"
	     placeholder="楼宇" clearable>
	      <el-option
	       v-for="item in ListFloorArr"
	       :key="item.id"
	       :label="item.floorName"
	       :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="房屋" prop="roomId">
	    <el-select v-model="queryParams.roomId"
	     class="width100"
	     placeholder="房屋" clearable>
	      <el-option
	        v-for="item in RoomList"
	        :key="item.id"
	        :label="item.communityName +''+ item.floorName +''+ item.units +'单元'+ item.roomNo +'号'"
	        :value="item.id"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="上门时间">
	    <el-date-picker
	      v-model="costTime"
	      value-format="yyyy-MM-dd HH:mm:ss"
	      type="daterange"
	      range-separator="-"
	      start-placeholder="开始日期"
	      end-placeholder="结束日期"
	    ></el-date-picker>
	  </el-form-item>
	  <el-form-item label="处理状态" prop="serveStatus">
	    <el-select v-model="queryParams.serveStatus"
	     class="width100"
	     placeholder="处理状态" clearable>
	      <el-option
	       v-for="item in serveStatus"
	       :key="item.type"
	       :label="item.name"
	       :value="item.type"
	      />
	    </el-select>
	  </el-form-item>
	  <el-form-item label="服务类型" prop="serveType">
	    <el-select v-model="queryParams.serveType"
	     class="width100"
	     placeholder="服务类型" clearable>
	      <el-option
	       v-for="item in serveType"
	       :key="item.type"
	       :label="item.name"
	       :value="item.type"
	      />
	    </el-select>
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" 
		
	> 
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" prop="id" />
	  <el-table-column label="房屋" align="center" prop="room" width="200" />
      <el-table-column label="申请人" align="center" prop="applicantUser" />
      <el-table-column label="申请时间" align="center" prop="cost" width="200" />
      <el-table-column label="服务类型" align="center" prop="serveTypeCn" />
      <el-table-column label="处理状态" align="center" prop="serveStatusCn"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row)"
          >查看详情</el-button>
		  <el-button
		    size="mini"
		    type="text"
		    icon="el-icon-edit"
		    @click="handleUpdate(scope.row)"
		  >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
	<!-- 对话框开始 -->
	<el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
	  <el-form ref="form" :model="form" :rules="rules" label-width="120px">
		<h3>服务单信息</h3>
	    <el-form-item label="房屋" prop="roomId">
	      <el-select v-model="form.roomId"
	       class="width100"
	       placeholder="房屋" clearable>
	        <el-option
	          v-for="item in RoomList"
	          :key="item.id"
	          :label="item.communityName +''+ item.floorName +''+ item.units +'单元'+ item.roomNo +'号'"
	          :value="item.id"
	        />
	      </el-select>
		</el-form-item>
		<el-form-item label="申请人" prop="applicantUser">
		 <el-input placeholder="申请人" 
		 	v-model="form.applicantUser">
		   </el-input>
		</el-form-item>
		<el-form-item label="服务类型" prop="serveType">
		  <el-select v-model="form.serveType"
		   class="width100"
		   placeholder="服务类型" clearable>
		    <el-option
		      v-for="item in serveType"
		      :key="item.type"
		      :label="item.name"
		      :value="item.type+''"
		    />
		  </el-select>
		</el-form-item>
		<el-form-item label="服务状态" prop="serveStatus">
		  <el-select v-model="form.serveStatus"
		   class="width100"
		   placeholder="服务状态" clearable>
		    <el-option
		      v-for="item in serveStatus"
		      :key="item.type"
		      :label="item.name"
		      :value="item.type+''"
		    />
		  </el-select>
		</el-form-item>
		<el-form-item label="期望上门时间" prop="costTime">
			 <el-date-picker
			   v-model="form.costTime"
			   value-format="yyyy-MM-dd HH:mm:ss"
			   class="width100"
			   type="daterange"
			   range-separator="-"
			   start-placeholder="开始时间"
			   end-placeholder="结束时间"
			 ></el-date-picker>
		</el-form-item>
		<el-form-item label="问题描述" prop="remark">
			<el-input v-model="form.remark" placeholder="问题描述" type="textarea" />
		</el-form-item>
		<el-form-item label="相关照片" prop="img">
			<FileUpload
				@input='inputFile'
				:value="fileList"
				:fileType="['png', 'jpg', 'jpeg']"
			>
			</FileUpload>
		</el-form-item>
		<h3>缴费信息</h3>
		<el-form-item label="缴费金额" prop="payAmount">
		 <el-input placeholder="请输入缴费金额" type='number'
		 	v-model="form.payAmount">
		     <template slot="append">元</template>
		   </el-input>
		</el-form-item>
		<el-form-item label="缴费方式" prop="payWay">
		  <el-select v-model="form.payWay"
		   class="width100"
		   placeholder="缴费方式" clearable>
		    <el-option
		      v-for="item in payWay"
		      :key="item.type"
		      :label="item.name"
		      :value="item.type+''"
		    />
		  </el-select>
		</el-form-item>
		<el-divider></el-divider>
		<div v-if='form.look'>
			<h3>缴费详情</h3>
			<el-form-item label="缴费金额" prop="payAmount">
			 <el-input placeholder="请输入缴费金额" type='number'
			 	v-model="form.payAmount">
			     <template slot="append">元</template>
			   </el-input>
			</el-form-item>
			<el-form-item label="缴费方式" prop="payAmount">
			   微信
			</el-form-item>
			<el-form-item label="缴费时间" prop="payTime" v-if='form.payTime'>
			 <el-input placeholder="请输入缴费时间" 
			 	v-model="form.payTime">
			   </el-input>
			</el-form-item>
			<el-form-item label="订单号" prop="payNo" v-if='form.payNo'>
			 <el-input placeholder="订单号" 
			 	v-model="form.payTime">
			   </el-input>
			</el-form-item>
		</div>
	  </el-form>
	  <div slot="footer" class="dialog-footer" v-if='!form.look'>
	    <el-button type="primary" @click="submitForm">确 定</el-button>
	    <el-button @click="cancel">取 消</el-button>
	  </div>
	</el-dialog>
	<!--对话框结束-->
  </div>
</template>
<script>
import { listPropertyServe, delPropertyServe, insertPropertyServe, editPropertyServe} from "@/api/estatemanagement/order";
import FileUpload from '@/components/FileUpload'
export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
	  //审核弹窗
	  openStatus:false,
      // 遮罩层
      loading: true,
	  //上传图片
	  fileList:[],
	  //账期
	  costTime:[],
      // 选中数组
      ids: [],
	  //所属小区
	  communityArr:[],
	  //所有楼宇
	  ListFloorArr:[],
	  //所有房间
	   RoomList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
		expectBeginTime: undefined,
        expectEndTime: undefined,
        serveType: undefined,
        floorId: undefined,
        serveStatus: undefined,
        roomId: undefined,
      },
      // 表单参数
      form: {
		  roomStatus:2
	  },
	  formStaus: { roomStatus:2 },
	  rulesStatus:{
		  custodyCycle: [
		    { required: true, message: "托管周期不能为空", trigger: "blur" }
		  ],
	  },
      // 表单校验
      rules: {
        roomId: [
          { required: true, message: "房屋不能为空", trigger: "blur" }
        ],
       costTime: [
         { required: true, message: "期望上门时间不能为空", trigger: "blur" }
       ],
	   applicantUser: [
	     { required: true, message: "申请人不能为空", trigger: "blur" }
	   ],
	   serveStatus: [
	     { required: true, message: "服务状态不能为空", trigger: "blur" }
	   ],
	   serveType: [
	     { required: true, message: "服务类型不能为空", trigger: "blur" }
	   ],
	   serveType: [
	     { required: true, message: "服务类型不能为空", trigger: "blur" }
	   ],
	   serveType: [
	     { required: true, message: "服务类型不能为空", trigger: "blur" }
	   ],
      }
    };
  },
  created() {
	this.getListFloorArr();
	this.getRoomList()
  },
  computed: {
	  serveStatus: {
	    get() {
	      return this.$store.state.common.serveStatus
	    },
	  },
	  serveType: {
	  	get() {
	  	  return this.$store.state.common.serveType
	  	},
	  },
	  payWay: {
	  	get() {
	  	  return this.$store.state.common.payWay
	  	},
	  },
  },
  methods: {
	//获取上传图片
	inputFile(file){
		this.form.images = file
	},
	//获取所有房间
	getRoomList(){
		this.$store.dispatch('AllRoomList').then(res => {
			 this.RoomList = res
			 this.getList();
		})
	},
	//获取所有楼宇
	getListFloorArr(){
		this.$store.dispatch('AllFloorList').then(res => {
			 this.ListFloorArr = res
		})
	},
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPropertyServe(this.queryParams).then(response => {
        this.postList = response.rows.map(i=>{
			let room = this.RoomList.filter(v=>v.id == i.roomId)[0]
			i.cost = i.expectBeginTime.split('00')[0]+' - '+ i.expectEndTime.split('00')[0]
			i.serveStatusCn = this.serveStatus.filter(v=>v.type == i.serveStatus)[0]['name']
			i.serveTypeCn = this.serveType.filter(v=>v.type == i.serveType)[0]['name']
			i.serveType = `${i.serveType}`
			i.serveStatus = `${i.serveStatus}`
			i.payWay = `${i.payWay}`
			i.room = room.communityName +''+ room.floorName +''+ room.units +'单元'+ room.roomNo +'号'
			return i
		})
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
	  this.openStatus = false;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
	  this.form = {}
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
	  this.queryParams.expectBeginTime = this.costTime[0]
	  this.queryParams.expectEndTime = this.costTime[1]
      this.getList();
    },
	/** 审核状态 */
	handleEditStatus(res){
		const postId = res.id
		this.formStaus.roomId = postId
		this.openStatus = true;
	},
    /** 重置按钮操作 */
    resetQuery() {
	  this.costTime = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const postId = row.id
	  this.fileList = row.images
      this.form = row;
	  this.form.costTime = [row.expectBeginTime,row.expectEndTime]
      this.open = true;
      this.title = "修改服务单";
	  this.form.look = false
    },
	handleLook(row){
		this.handleUpdate(row)
		this.form.look = true
		this.title = "查看服务单";
	},
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
		  this.form.expectBeginTime = this.form.costTime[0]
		  this.form.expectEndTime = this.form.costTime[1]
		  delete this.form.costTime
          if (this.form.id != undefined) {
            editPropertyServe(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertPropertyServe(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
	/** 审核提交按钮 */
    submitFormStaus: function() {
      this.$refs["formStaus"].validate(valid => {
        if (valid) {
          Examine(this.formStaus).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.openStatus = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */ 
    handleDelete(row) {
      const postIds = {
		  id:row.id
	  };
      this.$modal.confirm('是否确认删除该物业服务订单？').then(function() {
        return delPropertyServe(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
